package com.android.hedgehog;

import java.util.List;

import android.graphics.Color;
import android.util.Log;

import com.android.hedgehog.RisingConnectionScreen.GameState;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input;
import com.android.hedgehog.framework.Screen;
import com.android.hedgehog.framework.Input.TouchEvent;

public class RisingConnect extends Screen {
	public enum GameState {
		Ready, Phase1, Phase2, GameOver, GameOver2
	}
	final static int totalTime = 44;
	double accumulatedTime = 0.0;
	double pauseTime = 0.0;
	int Time = 1;
	int totalScore;
	int total;
	Graphics g;
	int hitMiss;
	GameState state;
	Input input;
	float bufferTime;
	RisingNumbers boxes;
	public FingerTracer ft;
	int wrongBox;
	boolean win;

	public RisingConnect(Game game) {
		super(game);
		g = game.getGraphics();
		boxes = new RisingNumbers((int) ((Math.random() * 10) + 1));
		state = GameState.Ready;
		total = boxes.totalBoxes;
		ft = new FingerTracer(g, total);
	}

	@Override
	public void update(float deltaTime) {

		accumulatedTime += deltaTime;
		if (accumulatedTime > Time)
			Time++;
		if (state == GameState.Ready && accumulatedTime > 2.5)
			state = GameState.Phase1;
		if (Time > totalTime) {
			game.setScreen(new FreePlayResult(game, 1,totalScore));
		}

		if (state == GameState.Ready||state == GameState.Phase1||state==GameState.Phase2) {

			List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
			int len = touchEvents.size();

			for (int i = 0; i < len; i++) {
				TouchEvent event = touchEvents.get(i);
				if (event.type == TouchEvent.TOUCH_DOWN
						|| event.type == TouchEvent.TOUCH_DRAGGED) {

					if (state == GameState.Ready) {
						if ((event.x > 0 && event.x < 320))
							state = GameState.Phase2;
					}

					if (state == GameState.Phase1) {
						ft.line(event.x, event.y);
						for (int j = boxes.listOrder; j < boxes.choices.length; j++) {
							if (ft.ifHit(boxes.choices[j].xPosition,
									boxes.choices[j].yPosition, 40, 40)) {
								if (j == boxes.listOrder) {// correct
									boxes.listOrder++;
									if (boxes.listOrder == boxes.totalBoxes) {
										state = GameState.GameOver;
										totalScore += (10 + boxes.difficulty);
										bufferTime = (float) accumulatedTime;
										win = true;
									} else
										state = GameState.Phase2;
								} else {
									state = GameState.GameOver;
									totalScore -= 10;
									bufferTime = (float) accumulatedTime;
									wrongBox = j;
								}
								break;
							}
							if (ft.crossedOver) {
								state = GameState.GameOver2;
								totalScore -= 10;
								bufferTime = (float) accumulatedTime;
								ft.crossedOver = false;
							}
						}

					}// PHASE1
					if (state == GameState.Phase2)
						if (boxes.listOrder == 0) {
							if (boxes.choices[boxes.listOrder].xPosition < event.x
									&& boxes.choices[boxes.listOrder].xPosition + 40 > event.x
									&& boxes.choices[boxes.listOrder].yPosition < event.y
									&& boxes.choices[boxes.listOrder].yPosition + 40 > event.y) {
								ft.line(event.x, event.y);
								state = GameState.Phase1;
							}
						}
					if (boxes.listOrder > 0) {
						if (boxes.choices[boxes.listOrder - 1].xPosition < event.x
								&& boxes.choices[boxes.listOrder - 1].xPosition + 40 > event.x
								&& boxes.choices[boxes.listOrder - 1].yPosition < event.y
								&& boxes.choices[boxes.listOrder - 1].yPosition + 40 > event.y) {
							ft.line(event.x, event.y);
							state = GameState.Phase1;
						}// PHASE 2
					}
				}// DOWN && DRAG

				if (event.type == TouchEvent.TOUCH_UP) {// UP
					ft.reFresh(ft.currentXY);
					ft.flag = false;

				}// UP
			}
		} else if (state == GameState.GameOver) {
			if (accumulatedTime >= bufferTime + 1) {

				boxes = new RisingNumbers((int) ((Math.random() * 10) + 1));
				ft = new FingerTracer(g, boxes.totalBoxes);
				state = GameState.Phase2;
				win = false;
			}
		} else {
			if (accumulatedTime >= bufferTime + 1) {

				boxes = new RisingNumbers((int) ((Math.random() * 10) + 1));
				ft = new FingerTracer(g, boxes.totalBoxes);
				state = GameState.Phase2;
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		g.drawPixmap(Assets.blackbackground, 0, 0);

		if (state == GameState.Ready) {
			g.drawText("Connect boxes in " + "increasing order", 160, 240, 14,
					Color.WHITE);
			return;
		}

		g.drawText("" + (totalTime - Time), 280, 20, 20, Color.WHITE); // display
		// Time
		g.drawText("" + totalScore, 20, 20, 20, Color.WHITE); // display Score

		if (state == GameState.Phase1 || state == GameState.Phase2) {

			ft.drawLine(0, 31, 320, 449, Color.GREEN);
			for (int i = 0; i < boxes.choices.length; i++) {
				g.drawRect2(boxes.choices[i].xPosition,
						boxes.choices[i].yPosition, 40, 40, Color.GREEN);
				g.drawText(boxes.choices[i].string,
						boxes.choices[i].xPosition + 20,
						boxes.choices[i].yPosition + 28, 20, Color.BLUE);
			}
			for (int i = 0; i < boxes.listOrder; i++) {
				g.drawRect(boxes.choices[i].xPosition,
						boxes.choices[i].yPosition, 40, 40, Color.GREEN);
				g.drawText(boxes.choices[i].string,
						boxes.choices[i].xPosition + 20,
						boxes.choices[i].yPosition + 28, 20, Color.BLACK);
			}

		}
		if (state == GameState.GameOver && win == false) {
			g.drawRect(boxes.choices[wrongBox].xPosition,
					boxes.choices[wrongBox].yPosition, 40, 40, Color.RED);
			g.drawText(boxes.choices[wrongBox].string,
					boxes.choices[wrongBox].xPosition + 20,
					boxes.choices[wrongBox].yPosition + 28, 20, Color.BLACK);
			g.drawRect(boxes.choices[boxes.listOrder].xPosition,
					boxes.choices[boxes.listOrder].yPosition, 40, 40,
					Color.GREEN);
			g.drawText(boxes.choices[boxes.listOrder].string,
					boxes.choices[boxes.listOrder].xPosition + 20,
					boxes.choices[boxes.listOrder].yPosition + 28, 20,
					Color.BLACK);
		}
		if (state == GameState.GameOver2) {
			g.drawText("you crossed the line", 160, 230, 15, Color.WHITE);
		}

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
